<?php
namespace app\webadmin\controller;
use think\Db;
use think\Request;
use app\webadmin\model\Cate;
use app\webadmin\model\Article as article;
class Knowledge extends Base
{

    public $cate = '';
    public $catelst = '';
    public $tags = [];
    public function _initialize(){
        $this->tags = $this->getTags();
		$this->article = new article();
    }

    public function index(Request $request){
        $topcate_id = $request->param('topcate_id');
        $cid = $request->param('cid');
        if(empty($topcate_id) || empty($cid)){
            return array('code'=>502,'msg'=>'参数错误');
        }
        $where = [
            'topcate_id'=>$topcate_id,
            'cid'=>$cid
        ];
        $lst = Db::name('table_article')->where($where)->order('updatetime','desc')->paginate(10);
        $page = $lst->render();
        $this->assign(['lst'=>$lst,'page'=>$page,'modelcate'=>$where]);
        return $this->fetch();
    }

    // insertitem.html 新增数据
    public function insertitem(Request $request){
        if($request->isPost()){
			$data = $request->param('');
			if(!$data['topcate'] || !$data['cate'] || !$data['title']){
				return array('code'=>502,'msg'=>'参数错误');
			}
			//参数
            $param = [
                'topcate_id'=>$data['topcate'],
                'issuetime'=>$data['issue_time']?strtotime($data['issue_time']):date('Y-m-d H:i:s',time()),
                'cid'=>$data['cate'],
                'is_goog'=>$data['default_like']?$data['default_like']:0,
                'is_bad'=>$data['default_hate']?$data['default_hate']:0,
				'clicktime'=>$data['clicktime']?$data['clicktime']:0,
                'author'=>$data['author']?$data['author']:'admin',
                'desc'=>$data['desc']?$data['desc']:'',
                'title'=>$data['title'],
				'tid'=>$data['tagString'],
                'content'=>$request->param('editorValue','')
            ];
			//在模型层进行数据操作
			$insert = $this->article->insertArticle($param);
			return $insert;
        }else{
            $param = $request->param();
            if(empty($param['cate'] || empty($param['topcate_id']))){
                return array('code'=>504,'msg'=>'请求错误');
            }
			$cateTable = new Cate;
			$catelst = $cateTable->catelst($param['topcate_id']);
            $cate = $cateTable->getcate($param['cate']);
            $this->assign([
                'catelst'=>$catelst,
                'cate'=>$cate,
                'tags'=>$this->tags
            ]);
            return $this->fetch();
        }
    }
}
